package cn.sumpu.app.guoguojie.dao.impl;

import java.util.List;
import java.util.Map;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import cn.sumpu.app.guoguojie.dao.DoingDao;
import cn.sumpu.app.guoguojie.domain.Doing;
import cn.sumpu.app.guoguojie.exception.AppBusinessException;

public class DoingDaoImpl extends SqlSessionDaoSupport implements DoingDao {
	public void insert(Doing doing) throws AppBusinessException {
		try {
			getSqlSession().insert(
					"cn.sumpu.app.guoguojie.persistence.DoingMapper.insert",
					doing);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("Write database failure.", e);
			}
			throw new AppBusinessException("Write database failure.", e);
		}
	}

	public void update(Doing doing) throws AppBusinessException {
		try {
			getSqlSession().update(
					"cn.sumpu.app.guoguojie.persistence.DoingMapper.update",
					doing);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("update database failure.", e);
			}
			throw new AppBusinessException("update database failure.", e);
		}

	}

	public Doing getDoingById(String id) throws AppBusinessException {
		try {
			return (Doing) getSqlSession()
					.selectOne(
							"cn.sumpu.app.guoguojie.persistence.DoingMapper.getDoingById",
							id);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("query database failure.", e);
			}
			throw new AppBusinessException("query database failure.", e);
		}
	}
	
	
	
	@Override
	public Doing getRootDoingById(String id) throws AppBusinessException {
		try {
			return (Doing) getSqlSession()
					.selectOne("cn.sumpu.app.guoguojie.persistence.DoingMapper.getRootDoingById",
							id);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("query database failure.", e);
			}
			throw new AppBusinessException("query database failure.", e);
		}
	}

	public Doing getDoingByFamily(String family) throws AppBusinessException {
		try {
			return (Doing) getSqlSession()
					.selectOne(
							"cn.sumpu.app.guoguojie.persistence.DoingMapper.getDoingByFamily",
							family);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("query database failure.", e);
			}
			throw new AppBusinessException("query database failure.", e);
		}
	}
	
	public Doing getDoingByMember(String member) throws AppBusinessException {
		try {
			return (Doing) getSqlSession()
					.selectOne(
							"cn.sumpu.app.guoguojie.persistence.DoingMapper.getDoingByMember",
							member);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("query database failure.", e);
			}
			throw new AppBusinessException("query database failure.", e);
		}
	}

	@SuppressWarnings("unchecked")
	public List<Doing> getDoingList(Map<String, Object> map)
			throws AppBusinessException {
		try {
			return getSqlSession()
					.selectList(
							"cn.sumpu.app.guoguojie.persistence.DoingMapper.getDoingList",
							map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("query database failure.", e);
			}
			throw new AppBusinessException("query database failure.", e);
		}
	}
	
	

	@Override
	public int getDoingListCount(Map<String, Object> map)
			throws AppBusinessException {
		try {
			Object obj = getSqlSession().selectOne("cn.sumpu.app.guoguojie.persistence.DoingMapper.getDoingListCount",map);
			return (Integer) (obj==null?0:obj);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("query database failure.", e);
			}
			throw new AppBusinessException("query database failure.", e);
		}
	}

	public void deleteDoing(Map<String,Object> map) throws AppBusinessException {
		try {
			 getSqlSession()
					.delete(
							"cn.sumpu.app.guoguojie.persistence.DoingMapper.delete",
							map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("query database failure.", e);
			}
			throw new AppBusinessException("query database failure.", e);
		}

	}

	@Override
	public void updateForwards(String doingId) throws AppBusinessException {
		try {
			 getSqlSession()
					.delete(
							"cn.sumpu.app.guoguojie.persistence.DoingMapper.updateForwards",
							doingId);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("update database failure.", e);
			}
			throw new AppBusinessException("update database failure.", e);
		}

	}
	
	
}
